package ch.elektroplan.mueller.passwordSave.business.supporters;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import android.content.Context;
import android.widget.Toast;
import ch.elektroplan.mueller.passwordSave.business.exeptions.PWSFalseOrderExeption;
import ch.elektroplan.mueller.passwordSave.business.exeptions.PWSLoaderExeption;
import ch.elektroplan.mueller.passwordSave.business.interfaces.ILoader;

public class FileLoader implements ILoader {

	private Context context;
	private File namefile;

	public FileLoader(Context context) {
		this.context = context;
	}

	public String[] getStringsArr() throws PWSFalseOrderExeption{
		
		if (namefile== null) {
			throw new PWSFalseOrderExeption(context, "false  order: set File first"); 
		}
		
		FileReader fileReader = null;
		String[] strings = new String[100];

		
		try {
			fileReader = new FileReader(namefile);
		} catch (FileNotFoundException e) {
			Toast.makeText(context, "NamaloaderFilereader failed",
					Toast.LENGTH_SHORT);
		}

		Scanner scanner = new Scanner(fileReader);
		
		int position = 0;	
		
		while (scanner.hasNext()) {
			String input = scanner.nextLine();
			if (input != null) {
				strings[position] = input;
				position++;
			}
		}
		
		String[] temp = new String[position];
		int i = 0;
		while (strings[i]!= null) {
			temp[i] = strings[i];
			i++;
		}
		

		return temp;
	}

	public boolean searchFile(String filePath) throws PWSLoaderExeption {

		namefile = new File(filePath);

		if (namefile == null) {
			throw new PWSLoaderExeption(context, "searchfile failed");
		} else if (namefile.isFile() && namefile.canRead()) {
			return true;
		} else {
			return false;
		}

	}

}
